package com.easy.controller;

import com.easy.bean.Course;
import com.easy.bean.Member;
import com.easy.bean.Trainer;
import com.easy.common.CommonResult;
import com.easy.service.ICourseService;
import com.easy.service.ITrainerService;
import com.easy.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
public class TrainerController {

    @Autowired
    ITrainerService trainerS;

    @Autowired
    ICourseService courseS;

    @GetMapping("trainer")
    public CommonResult getTrainer(Trainer trainer, Page page){
        List<Trainer> list = trainerS.getTrainer(trainer, page);
        int count = trainerS.getTrainerCount(trainer);
        HashMap map = new HashMap<>();
        map.put("list", list);
        map.put("count", count);
        return CommonResult.success(map);
    }

    @DeleteMapping("trainer/{trainerID}")
    public CommonResult delTrainer(@PathVariable int trainerID){
        List<Course> list = courseS.getAllCourse();
        for(Course c : list){
            if(c.getTrainerID() == trainerID){
                return CommonResult.fail(400,"教练有课，不能删除");
            }
        }
        int res = trainerS.delTrainer(trainerID);
        return CommonResult.success(res);
    }

    @PostMapping("trainer")
    public CommonResult addTrainer(@RequestBody Trainer trainer){
        List<Trainer> list = trainerS.getAllTrainer();
        for(Trainer t : list){
            if(t.getPhoneNumber().equals(trainer.getPhoneNumber())){
                return CommonResult.fail(400,"该电话号码已存在");
            } else if (t.getEmail().equals(trainer.getEmail())) {
                return CommonResult.fail(400,"该电子邮箱已存在");
            }else if (t.getNationalIDNumber().equals(trainer.getNationalIDNumber())) {
                return CommonResult.fail(400,"该身份证号已存在");
            }
        }
        trainerS.addTrainer(trainer);
        trainer = trainerS.getTrainerByID(trainer.getTrainerID());
        return CommonResult.success(trainer);
    }

    @PutMapping("trainer")
    public CommonResult editTrainer(@RequestBody Trainer trainer){
        List<Trainer> list = trainerS.getAllTrainer();
        for(Trainer t : list){
            if(t.getPhoneNumber().equals(trainer.getPhoneNumber())&&t.getTrainerID() != trainer.getTrainerID()){
                return CommonResult.fail(400,"该电话号码已存在");
            } else if (t.getEmail().equals(trainer.getEmail())&&t.getTrainerID() != trainer.getTrainerID()) {
                return CommonResult.fail(400,"该电子邮箱已存在");
            }else if (t.getNationalIDNumber().equals(trainer.getNationalIDNumber())&&t.getTrainerID() != trainer.getTrainerID()) {
                return CommonResult.fail(400,"该身份证号已存在");
            }
        }
        trainerS.editTrainer(trainer);
        trainer = trainerS.getTrainerByID(trainer.getTrainerID());
        return CommonResult.success(trainer);
    }

    @GetMapping("alltrainer")
    public CommonResult getAllTrainer(){
        List<Trainer> list = trainerS.getAllTrainer();
        return CommonResult.success(list);
    }
}
